package com.zjc.yohoshop.mvp.view.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

/**
 * msg.自定义首字母导航View
 */
public class LetterView extends View {

    private String[] letters;//要显示的首字母数组
    private SelectLetterListener listener;
    private int width = 0;//当前控件的宽度
    private int height = 0;//当前控件的高度
    private int pandding = 0;//当前控件上下边距->一个字母的高度
    private int letterHeight = 0;//字母高度
    private int spaceHeight = 0;//字母间隔高度
    private Paint paint;//绘制文字画笔

    private boolean pressFlag = false;//按下标志->pressFlag=true按下;pressFlag=false抬起
    private int lastIndex = -1;
    private int index;

    public LetterView(Context context, AttributeSet attrs) {
        super(context, attrs);

        setLetters();

        /**
         * 初始化画笔
         */
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setColor(Color.parseColor("#909090"));//设置画笔颜色黑
        paint.setTextAlign(Paint.Align.CENTER);//居中绘制

    }



    /**
     * 初始化字母数组
     */
    private void setLetters() {
        letters = new String[27];
        int num = 0;
        for (int i = 'A'; i <= 'Z'; i++){
            letters[num++] = "" + ((char)i);
        }
        letters[26] = "0";
    }

    /**
     * 获取监听
     * @param listener
     */
    public void setListener(SelectLetterListener listener) {
        this.listener = listener;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        if (height == 0){
            //第一次进入绘制方法->获取控件的宽以及高度
            height = getMeasuredHeight();
            width = getMeasuredWidth();
//            pandding = height/letters.length;//获取上下文边距
//            int textHeight = (height - pandding * 2) / letters.length;
            int textHeight = height / letters.length;
            letterHeight = textHeight / 4 * 3;
            spaceHeight = textHeight / 4;
//            letterHeight = pandding / 4 * 2;//算出字体大小
//            spaceHeight = pandding / 4 * 2;//剩余空间
            paint.setTextSize(letterHeight);//设置字体大小
        }
            canvas.drawColor(Color.TRANSPARENT);

        /**
         * 遍历绘制文字
         */
        for (int i = 0; i < letters.length; i++){
            float y = (i + 1) * (letterHeight + spaceHeight) ;
            if (i == index && pressFlag){
                paint.setColor(Color.BLACK);
            }else{
                paint.setColor(Color.parseColor("#909090"));
            }
            canvas.drawText(letters[i], width / 2, y, paint);
        }

    }

    /**
     * 处理手指按下移动抬起
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                if (listener == null){
                    return super.onTouchEvent(event);
                }
                if (event.getX() < 0){
                    //按到控件外->不需要做相关处理
                    pressFlag = false;
                    return super.onTouchEvent(event);
                }
                lastIndex = -1;
                /**
                 * 改变按下标志
                 */
                pressFlag = true;
                invalidate();
                setPop(event);

                break;
            case MotionEvent.ACTION_MOVE:
                if (event.getX()<0 && event.getY() >= 0 && event.getY() <= getHeight()){
                    //按到控件外->不需要做相关处理
//                    pressFlag = false;
                    setPop(event);
                    invalidate();
                    return super.onTouchEvent(event);
                }
                if (event.getX() >= 0 && event.getX() <= getWidth()  && event.getY() >= 0 && event.getY() < getHeight()){
                    setPop(event);
                    invalidate();
                    return super.onTouchEvent(event);
                }
                break;
            case MotionEvent.ACTION_UP:
                /**
                 * 改变按下标志
                 */
                pressFlag = false;
                /**
                 * 重新绘制
                 */
                invalidate();
                /**
                 * 关闭PopupWindow
                 */
                break;
        }
        return true;
    }

    /**
     * 根据当前点击的Y坐标判断所在首字母区域
     * @param event
     */
    private void setPop(MotionEvent event){
        int y = (int) event.getY();//获取当前所在坐标
        int more = y % (letterHeight + spaceHeight);
        index = (y - more) / (letterHeight + spaceHeight);
        if (index != lastIndex ){
            if (index >= 27){
                index = 26;
            }
            listener.selectLetter(letters[index]);
            lastIndex = index;
        }
    }

    /**
     * 回调接口->向Activity或者Fragment传递当前滑动选中的那一条
     * 用来做联动
     */
    public interface SelectLetterListener{
        void selectLetter(String letter);
    }

}
